package cn.passlink.www.Controller;

import cn.passlink.www.Enity.sysUser;
import cn.passlink.www.Server.ArticleServer;
import cn.passlink.www.Server.sysUserServer;
import cn.passlink.www.Utils.JsonUtils;
import cn.passlink.www.Utils.JwtUtils;
import cn.passlink.www.Utils.ReqDataCheck;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class sysUserController {

    @Resource
    private sysUserServer UserService;

    /**
     * getContact
     * 获取用户简介
     *
     * @param article_id
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @GetMapping("/getContact")
    public Map<String, Object> getContact(@RequestParam("article_id") String article_id) {

        if (article_id.length() < 8) {
            log.error("用户id错误");
            return JsonUtils.resultJson(403, "获取用户信息失败！！！");
        }
        return UserService.getContact(article_id);

    }

    /**
     * detials
     * 获取用户详细信息，需要验证
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author passlink
     * @create 2023/10/14
     **/

    @GetMapping("/detials")
    public Map<String, Object> detials(HttpServletRequest request) {
        //获取accessToken
        String accessToken = request.getHeader("Authorization");
        //        未登录
        if (!StringUtils.hasText(accessToken) || !accessToken.startsWith("Bearer ")) {
            log.error("accessToken不存在");
            return JsonUtils.resultJson(403, "权限不足");
        }

        accessToken = accessToken.substring(7); // 去除前面的"Bearer "
        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            log.error("解析accessToken错误");
            return JsonUtils.resultJson(403, "权限不足");
        }
        return UserService.getDetials(user.getUser_id());

    }


    //修改用户信息，需要验证
    @PostMapping("/edit")
    public Map<String, Object> edit(@RequestBody Map<String, Object> requestData, HttpServletRequest request) {
        //获取accessToken
        String accessToken = request.getHeader("Authorization");

        if (!StringUtils.hasText(accessToken) || !accessToken.startsWith("Bearer ")) {
            log.error("accessToken不存在");
            return JsonUtils.resultJson(403, "权限不足");
        }

        String[] keys = {"avatar", "career_tags", "nickname", "introduction", "background_image"};
        if (!ReqDataCheck.ReqDataCheckKeys(requestData, keys)) {
            log.error("请求数据错误");
            return JsonUtils.resultJson(403, "请求数据错误");
        }

        accessToken = accessToken.substring(7); // 去除前面的"Bearer "

        //解析accessToken内的信息
        sysUser user = JwtUtils.getClaims(accessToken);
        if (user == null) {
            log.error("解析accessToken错误");
            return JsonUtils.resultJson(403, "权限不足");
        }

        String avatar = (String) requestData.get("avatar");
        String career_tags = (String) requestData.get("career_tags");
        String nickname = (String) requestData.get("nickname");
        String introduction = (String) requestData.get("introduction");
        String background_image = (String) requestData.get("background_image");
//        检查是否有空值，忽略图像

        if (introduction.isEmpty() || nickname.isEmpty() || career_tags.isEmpty()) {
            log.error("上传信息为空");
            return JsonUtils.resultJson(403, "上传信息为空！！！");
        }
        if (avatar.length() > 200 || career_tags.length() > 40 || nickname.length() > 12 || introduction.length() > 500 || background_image.length() > 200) {
            log.error("上传信息长度错误");
            return JsonUtils.resultJson(403, "此处不需要太多信心，简洁为好！！");

        }
        return UserService.updateDetials(user.getUser_id(), avatar, career_tags, nickname, introduction, background_image);
    }

    @GetMapping("/userCounts")
    public Map<String, Object> userCounts() {

        return UserService.getUserCounts();
    }
}
